<template>
  <a-modal :title="title" :width="400" :visible="visible" :confirmLoading="submitLoading" @ok="handleOk"
    @cancel="visible = false" ok-text="完成选择">
    <div v-if="treeData.length > 0">
      <a-tree v-model="checkedKeys" :tree-data="treeData" :defaultExpandedKeys="expandedKeys"
        :replaceFields="{ title: 'orgName', key: 'orgId' }" @check="checkClick" checkStrictly checkable>
      </a-tree>
    </div>
    <a-empty v-show="treeData.length === 0" style="margin: 50px auto;" />
  </a-modal>
</template>

<script>
import {
  orgGetMenuTree
} from '@/api/human/relationOrg'
export default {
  name: 'staffTree',
  data() {
    return {
      title: "选择员工",
      visible: false,
      enterpriseId: "",
      submitLoading: false,

      treeData: [{
        "orgId": "147031331636121600",
        "orgName": "机关单位",
        "parentOrgId": "91153104867037184",
        "parentOrgName": "冉祥",
        "staffList": [],
        "children": [
          {
            "orgId": "147031386292097024",
            "orgName": "组织部",
            "parentOrgId": "147031331636121600",
            "parentOrgName": "机关单位",
            "staffList": [],
            "children": []
          },
          {
            "orgId": "147031414301659136",
            "orgName": "宣传部",
            "parentOrgId": "147031331636121600",
            "parentOrgName": "机关单位",
            "staffList": [],
            "children": []
          }
        ]
      },
      {
        "orgId": "147031522015580160",
        "orgName": "事业单位",
        "parentOrgId": "91153104867037184",
        "parentOrgName": "冉祥",
        "staffList": [
          {
            "orgId": "147031522015580160",
            "orgName": "事业单位",
            "staffName": "李玉峰",
            "staffId": "147052781600378880"
          }
        ],
        "children": [
          {
            "orgId": "147031560745783296",
            "orgName": "电力系统",
            "parentOrgId": "147031522015580160",
            "parentOrgName": "事业单位",
            "staffList": [
              {
                "orgId": "147031560745783296",
                "orgName": "电力系统",
                "staffName": "李玉峰",
                "staffId": "147052781600378880"
              },
              {
                "orgId": "147031560745783296",
                "orgName": "电力系统",
                "staffName": "吴美佳2",
                "staffId": "150213539238645761"
              }
            ],
            "children": [
              {
                "orgId": "147031652357771264",
                "orgName": "汪波",
                "parentOrgId": "147031560745783296",
                "parentOrgName": "电力系统",
                "staffList": [
                  {
                    "orgId": "147031652357771264",
                    "orgName": "汪波",
                    "staffName": "李玉峰",
                    "staffId": "147052781600378880"
                  }
                ],
                "children": []
              }
            ]
          },
          {
            "orgId": "147031589199941632",
            "orgName": "人力系统",
            "parentOrgId": "147031522015580160",
            "parentOrgName": "事业单位",
            "staffList": [
              {
                "orgId": "147031589199941632",
                "orgName": "人力系统",
                "staffName": "李玉峰",
                "staffId": "147052781600378880"
              },
              {
                "orgId": "147031589199941632",
                "orgName": "人力系统",
                "staffName": "吴美佳2",
                "staffId": "150213539238645761"
              }
            ],
            "children": [
              {
                "orgId": "147031625606500352",
                "orgName": "李想",
                "parentOrgId": "147031589199941632",
                "parentOrgName": "人力系统",
                "staffList": [
                  {
                    "orgId": "147031625606500352",
                    "orgName": "李想",
                    "staffName": "李玉峰",
                    "staffId": "147052781600378880"
                  }
                ],
                "children": []
              }
            ]
          }
        ]
      },
      {
        "orgId": "147031700474826752",
        "orgName": "人力单位",
        "parentOrgId": "91153104867037184",
        "parentOrgName": "电力系统",
        "staffList": [
          {
            "orgId": "147031700474826752",
            "orgName": "人力单位",
            "staffName": "李玉峰",
            "staffId": "147052781600378880"
          },
          {
            "orgId": "147031700474826752",
            "orgName": "/人力单位",
            "staffName": "小五",
            "staffId": "150209194631761920"
          },
          {
            "orgId": "147031700474826752",
            "orgName": "/人力单位",
            "staffName": "吴法大师傅3",
            "staffId": "150209194640150530"
          },
          {
            "orgId": "147031700474826752",
            "orgName": "/人力单位",
            "staffName": "金桔",
            "staffId": "150209194640150533"
          },
          {
            "orgId": "147031700474826752",
            "orgName": "/人力单位",
            "staffName": "吴美佳",
            "staffId": "150209194640150528"
          }
        ],
        "children": []
      }],
      checkedKeys: [],
      checkedNames: [],
      selectedKeys: [],
      expandedKeys: [],
    }
  },
  watch: {

  },
  methods: {
    add(id, treeKey, treeName) {
      this.visible = true;
      if (id) {
        this.enterpriseId = id;
        this.checkedKeys = [];
        this.checkedNames = [];
        this.expandedKeys = [];
        // this.initMenuTree(id)
      }
      if (treeKey.length > 0) {
        this.checkedNames = treeName;
        this.checkedKeys = treeKey;
        this.expandedKeys = treeKey;
      }
    },
    async initMenuTree(id) {
      const { code, data } = await orgGetMenuTree(id);
      if (code === '1') {
        this.treeData = data;
      }
    },
    checkClick(keys, e) {
      let array = [];
      if (e.checkedNodes.length > 0) {
        e.checkedNodes.forEach(item => {
          array.push(item.data.props.orgName)
        })
      }
      this.checkedNames = array;
    },
    handleOk() {
      let orgObj = {
        name: this.checkedNames,
        key: this.checkedKeys.checked
      }
      // console.log(this.checkedNames);
      this.visible = false;
      this.$emit('on-tree', orgObj)
    }
  }
}
</script>
